﻿using AhpilyServer;
using PokerServer.Logic;
using Protocol.Code;
using System;

namespace PokerServer
{
    /// <summary>
    /// 网络的消息中心类
    /// 负责将下层网络的消息(from ClientPeer)分发到各个逻辑模块（Handler）
    /// </summary>
    public class NetMsgCenter : IApplication
    {
        readonly AccountHandler account = new AccountHandler();
        readonly UserHandler user = new UserHandler();
        readonly MatchHandler match = new MatchHandler();
        readonly ChatHandler chat = new ChatHandler();
        readonly FightHandler fight = new FightHandler();


        public NetMsgCenter()
        {
            match.startFight += fight.StartFight;
        }


        public void OnConnect(ClientPeer client)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 用户断开连接调用的方法
        /// </summary>
        /// <param name="client"></param>
        public void OnDisconnect(ClientPeer client)
        {
            // 顺序很重要*******************
            fight.OnDisconnect(client);
            chat.OnDisconnect(client);
            match.OnDisconnect(client);
            user.OnDisconnect(client);
            account.OnDisconnect(client);
        }

        public void OnReceive(ClientPeer client, SocketMsg msg)
        {
            switch (msg.OpCode)
            {
                case OpCode.ACCOUNT:
                    account.OnReceive(client, msg.SubCode, msg.Value);
                    break;
                case OpCode.USER:
                    user.OnReceive(client, msg.SubCode, msg.Value);
                    break;
                case OpCode.MATCH:
                    match.OnReceive(client, msg.SubCode, msg.Value);
                    break;
                case OpCode.CHAT:
                    chat.OnReceive(client, msg.SubCode, msg.Value);
                    break;
                case OpCode.FIGHT:
                    fight.OnReceive(client, msg.SubCode, msg.Value);
                    break;
                default:
                    Console.WriteLine("NetMsgCenter --- >  消息中心分发到逻辑层出现错误");
                    break;
            }
        }
    }
}